﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Threading;
using Microsoft.Maps.MapControl.WPF;
using WebService.Proxy;

namespace WpfClient
{
    public class MainPageViewModel : INotifyPropertyChanged
    {
		const string refreshMessageFormat = "Refresh in {0} sec";

        public MainPageViewModel(bool startTimer)
        {
            EarthquakeServiceProxy = new UsgsServiceProxy();
			if (startTimer)
				StartTimer();
        }

        public MainPageViewModel(IEarthquakeServiceProxy serviceProxy, bool startTimer)
        {
            EarthquakeServiceProxy = serviceProxy;
			if (startTimer)
				StartTimer();
        }

        #region INPC Implementation
        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChangedEvent(string propname)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propname));
        }
        #endregion

		public void StartTimer()
		{
			RefreshData();

			var timer = new DispatcherTimer();
			var count = 30;
			timer.Tick += (s, e) =>
				{
					RefreshMessage = String.Format(refreshMessageFormat, count--);
					if (count == 0)
					{
						count = 30;
						RefreshData();
					}
				};
			timer.Interval = new TimeSpan(0, 0, 1);
			timer.Start();
		}

        public IEarthquakeServiceProxy EarthquakeServiceProxy { get; private set; }

        private ObservableCollection<EarthquakeEvent> earthquakeData = new ObservableCollection<EarthquakeEvent>();
        public IEnumerable<EarthquakeEvent> EarthquakeData
        {
            get
            {
                return earthquakeData;
            }
            set
            {
                earthquakeData.Clear();
                foreach (var item in value)
                {
                    earthquakeData.Add(item);
                }
            }
        }

		private string refreshMessage = String.Format(refreshMessageFormat, 30);
		public string RefreshMessage
		{
			get { return refreshMessage; }
			set { refreshMessage = value; RaisePropertyChangedEvent("RefreshMessage"); }
		}

        private double zoomLevel = 3;
        public double ZoomLevel
        {
            get { return zoomLevel; }
            set
            {
                zoomLevel = value;
                RaisePropertyChangedEvent("ZoomLevel");
            }
        }

        private Location mapCenter;
        public Location MapCenter {
            get { return mapCenter ?? new Location(47.6063889,-122.3308333); }
            set
            {
                mapCenter = value;
                RaisePropertyChangedEvent("MapCenter");
            }
        }

        private EarthquakeEvent selectedEarthquakeEvent;
        public EarthquakeEvent SelectedEarthquakeEvent {
            get { return selectedEarthquakeEvent; }
            set
            {
                selectedEarthquakeEvent = value;
                ZoomLevel = 6;
                MapCenter = new Location((double)value.Latitude, (double)value.Longitude);
                RaisePropertyChangedEvent("SelectedEarthquakeEvent");
            }
        }

        public async Task RefreshData()
        {
            this.EarthquakeData = await EarthquakeServiceProxy.GetDataAsync();
        }

        private RelayCommand searchCommand;
        public ICommand SearchCommand
        {
            get
            {
                if (searchCommand == null)
                    searchCommand = new RelayCommand(arg =>
                    {
                        var view = new SearchView();
                        view.ShowDialog();
                    });

                return searchCommand;
            }
        }
    }
}
